package com.zerui.manager.access.auth.task.domain;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Getter
@Setter
public class AuthTask {
    /**
     * 权限组
     */
    private Long accessAuthId;
    /**
     * 操作（0 - 添加；1 - 删除）
     */
    private Integer operate;
    /**
     * 任务下发原因
     */
    private String reason;
    /**
     * 任务下发状态（0 - 未执行；1 - 执行中；2 - 执行失败）
     */
    private Integer syncStatus;
    /**
     * 任务详情
     */
    private List<AuthTaskDetail> details;

    public static AuthTask ofBindDevice(Long accessAuthId, Collection<Long> deviceIds, Collection<Long> personnelIds) {
        return of(accessAuthId, deviceIds, personnelIds, 0, "绑定设备");
    }

    public static AuthTask ofUnbindDevice(Long accessAuthId, Collection<Long> deviceIds, Collection<Long> personnelIds) {
        return of(accessAuthId, deviceIds, personnelIds, 1, "解绑设备");
    }

    public static AuthTask ofBindPersonnel(Long accessAuthId, Collection<Long> deviceIds, Collection<Long> personnelIds) {
        return of(accessAuthId, deviceIds, personnelIds, 0, "绑定人员");
    }

    public static AuthTask ofUnbindPersonnel(Long accessAuthId, Collection<Long> deviceIds, Collection<Long> personnelIds) {
        return of(accessAuthId, deviceIds, personnelIds, 1, "解绑人员");
    }

    public static AuthTask ofBindOrg(Long accessAuthId, Collection<Long> deviceIds, Collection<Long> personnelIds) {
        return of(accessAuthId, deviceIds, personnelIds, 0, "绑定部门");
    }

    public static AuthTask ofUnbindOrg(Long accessAuthId, Collection<Long> deviceIds, Collection<Long> personnelIds) {
        return of(accessAuthId, deviceIds, personnelIds, 1, "解绑部门");
    }

    public static AuthTask of(Long accessAuthId, Collection<Long> deviceIds, Collection<Long> personnelIds, Integer operate, String reason) {
        if (CollectionUtils.isEmpty(deviceIds) || CollectionUtils.isEmpty(personnelIds)) {
            return null;
        }
        AuthTask task = new AuthTask();
        task.accessAuthId = accessAuthId;
        task.operate = operate;
        task.reason = reason;
        task.syncStatus = 0;

        List<AuthTaskDetail> details = new ArrayList<>();
        for (Long deviceId : deviceIds) {
            for (Long personnelId : personnelIds) {
                details.add(AuthTaskDetail.of(accessAuthId, deviceId, personnelId, operate));
            }
        }
        task.details = details;
        return task;
    }
}
